home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-02 | 12.3 KB | 442 lines | [TEXT/MMCC] |
- /************************************************************************************
-
- File: NuMathComponent.c
-
- Contains: NuMath component routines.
-
- Written by: Gary Woodcock
-
- Copyright: © 1992 by Apple Computer, Inc.
-
- Change History (most recent first):
-
- Project settings for 68K
- Code model: Small, Smart or Large
- Link Single Segment: on (VERY important)
- Project type: Code Resource
- File name: NuMath_Component
- Sym name: NuMath_Component.SYM (not generated)
- Resource name: Extended Math Component
- Header type: Standard
- Multi Segment: on or off
- ResType: _68K
- ID: 132
- SegType blank (unused since we are linking single segment)
- Creator: gwck
- File type: thng
- Resource flags: SysHeap
- Project setup:
- Segment 1 (Main Segment): NuMathComponent.c
- Segment 2 (Group2): MacOS.lib, FatNuMathComponent.rsrc (See warning below)
- Segment 3 (Group3): and MathComponentCommon.c
-
- Project settings for PPC
- Project type: Code Resource
- File name: NuMath_Component
- Sym name: NuMath_Component.SYM (not generated)
- Resource name: Extended Math Component
- Header type: None (VERY important)
- ResType: _PPc
- ID: 130
- Creator: gwck
- File type: thng
- Resource flags: SysHeap
- Merge To File on
- Expand Uninitialized Data: on
- Main Entry Point MainRD (VERY important)
- Project setup:
- Group 1 (Main Group): NuMathComponent.c, PPCGlue.c
- Group 2 (Group2): InterfaceLib, MathLib, ToolsLib.o
- Group 3 (Group3): MathComponentCommon.c
-
- ***Warning*** FatNuMathComponent.rsrc has an extended thng resource which
- is set to require both PPC and 68K code. If you want to test the 68K
- component by itself, use NuMathComponent.rsrc, instead.
-
- CW components need to have the Link Single Segment switch checked. The Multi-
- segment switch can be checked or unchecked as fits your needs. (Please
- see the User Guide for which situations require the Multi-segment switch.)
- Putting everything into one segment eliminates segment loader errors that
- would occur if the startup code tried to call GetResource before
- OpenComponentResFile is called. Components written in C are moveable, components
- written in C++ with virtual functions probably aren't. Please see the
- C++ XCMD example on the CD for an example of how to deal C++ code resources.
-
- Project settings, port to PPC and comments by
- Mark Anderson
- metrowerks
- 12/1/94
-
- ************************************************************************************/
-
- //-----------------------------------------------------------------------
- // Includes
-
- #include "MathComponent.h"
- #include "MathComponentPrivate.h"
- #include "NuMathComponentPrivate.h"
- #include <FixMath.h>
- #ifndef powerc
- #include <A4Stuff.h>
- #endif
-
- //-----------------------------------------------------------------------
- // Prototype (can't be in header or there is a conflict with Component Tester)
- #ifndef DEBUG_IT
- pascal ComponentResult main (ComponentParameters *params,
- Handle storage);
- #endif
-
- //-----------------------------------------------------------------------
- //PPC Globals
- #ifdef powerc
- INSTANTIATE_ROUTINE_DESCRIPTOR(NuMathOpen);
- INSTANTIATE_ROUTINE_DESCRIPTOR(NuMathClose);
- INSTANTIATE_ROUTINE_DESCRIPTOR(NuMathCanDo);
- INSTANTIATE_ROUTINE_DESCRIPTOR(NuMathVersion);
- INSTANTIATE_ROUTINE_DESCRIPTOR(NuMathRegister);
- INSTANTIATE_ROUTINE_DESCRIPTOR(NuMathDoDivide);
- INSTANTIATE_ROUTINE_DESCRIPTOR(NuMathDoMultiply);
-
- #ifndef DEBUG_IT
- RoutineDescriptor MainRD = BUILD_ROUTINE_DESCRIPTOR(uppComponentRoutineProcInfo, main);
- ProcInfoType __procinfo = uppComponentRoutineProcInfo;
- #endif
- #endif
-
- //-----------------------------------------------------------------------
- #ifdef DEBUG_IT
-
- // Use this declaration when we're running linked (for debugging)
- pascal ComponentResult NuMathDispatcher (ComponentParameters *params,
- Handle storage)
-
- #else
-
- // Use this declaration when we're a standalone component
- pascal ComponentResult main (ComponentParameters *params,
- Handle storage)
-
- #endif DEBUG_IT
-
- {
- // This routine is the main dispatcher for the NuMath component
-
-
- ComponentResult result = noErr;
-
- #if !defined(powerc) && !defined(DEBUG_IT)
- long oldA4;
-
- oldA4 = SetCurrentA4();
- #endif
-
- // Did we get a Component Manager request code (< 0)?
- if (params->what < 0)
- {
- switch (params->what)
- {
- case kComponentOpenSelect: // Open request
- {
- result = CallComponentFunctionUniv(params, NuMathOpen);
- break;
- }
- case kComponentCloseSelect: // Close request
- {
- //if open fails, close gets called (why I don't know) so
- //globals aren't allocated
- if (!storage)
- return result;
- result = CallComponentFunctionWithStorageUniv(storage, params, NuMathClose);
- break;
- }
- case kComponentCanDoSelect: // Can Do request
- {
- result = CallComponentFunctionUniv(params, NuMathCanDo);
- break;
- }
- case kComponentVersionSelect: // Version request
- {
- result = CallComponentFunctionUniv(params, NuMathVersion);
- break;
- }
- case kComponentRegisterSelect: // Register request
- {
- result = CallComponentFunctionUniv(params, NuMathRegister);
- break;
- }
- case kComponentTargetSelect: // Target request not supported
- default: // Unknown request
- {
- result = paramErr;
- break;
- }
- }
- }
- else // Was it one of our request codes?
- {
- switch (params->what)
- {
- case kDoDivideSelect: // Divide request
- {
- result = CallComponentFunctionUniv(params, NuMathDoDivide);
- break;
- }
-
- case kDoMultiplySelect: // Multiply request
- {
- result = CallComponentFunctionWithStorageUniv(storage, params, NuMathDoMultiply);
- break;
- }
-
- default: // Unknown request
- {
- result = paramErr;
- break;
- }
- }
- }
-
- #if !defined(powerc) && !defined(DEBUG_IT)
- SetA4(oldA4);
- #endif
-
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult NuMathOpen (ComponentInstance self)
- {
- ComponentResult result = noErr;
- PrivateGlobals** globals;
-
- // Can we open another instance?
-
- if (CountComponentInstances ((Component) self) <= kMaxNuMathInstances)
- {
- // Did we get our storage?
- globals = (PrivateGlobals**) NewHandleClear (sizeof (PrivateGlobals));
- if (globals != nil)
- {
- ComponentDescription delegateDesc;
- Component delegateComponent = nil;
- ComponentInstance delegateComponentInstance;
- Component selfComponent;
- ComponentDescription tempDesc;
-
- // Keep a reference to self
- (*globals)->self = (Component) self;
-
- // Describe the component we want to capture
- delegateDesc.componentType = mathComponentType;
- delegateDesc.componentSubType = 0L;
- delegateDesc.componentManufacturer = 'appl';
- delegateDesc.componentFlags = 0L;
- delegateDesc.componentFlagsMask = 0L;
-
- // Keep track of which component we are by querying
- // the component info; the componentFlagsMask will
- // contain the component ID for the component we
- // are requesting info for
- result = GetComponentInfo ((Component) self, &tempDesc, nil, nil, nil);
- selfComponent = (Component)(tempDesc.componentFlagsMask);
-
- // Find the component we want to capture
- do
- {
- delegateComponent = FindNextComponent (delegateComponent, &delegateDesc);
- }
- while (delegateComponent == selfComponent);
-
- // Did we find one?
- if (delegateComponent != 0L)
- {
- // Can this component be captured (does it support the
- // target request code)?
- if (ComponentFunctionImplemented ((ComponentInstance) delegateComponent,
- kComponentTargetSelect))
- {
- // Capture it
- delegateComponent = CaptureComponent (delegateComponent,
- (Component) self);
-
- // Keep references to the component we captured
- (*globals)->delegateComponent = delegateComponent;
- delegateComponentInstance = OpenComponent (delegateComponent);
- (*globals)->delegateComponentInstance = delegateComponentInstance;
-
- // Did we get an instance of the component we captured?
- if (delegateComponentInstance != 0L)
- {
- // Inform the component it has been captured
-
- result = ComponentSetTarget (delegateComponentInstance, self);
- SetComponentInstanceStorage (self, (Handle) globals);
- }
- else // Couldn't get an instance of the delegate component
- {
-
- DisposHandle ((Handle) globals);
- result = kGenericError;
- }
- }
- else // The component we need can't be captured
- {
-
- DisposHandle ((Handle) globals);
- result = kGenericError;
- }
-
- }
- else // Couldn't find the delegate component
- {
- DisposeHandle ((Handle) globals);
- result = kGenericError;
- }
- }
- else // NewHandleClear failed
- {
-
- result = MemError();
- }
- }
- else // No more instances can be opened
- {
- result = kGenericError;
- }
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult NuMathClose (Handle storage,
- ComponentInstance self)
- {
- ComponentResult result = noErr;
- PrivateGlobals** globals = (PrivateGlobals**) storage;
-
- // Do we have any clean up to do?
- if (globals != nil)
- {
- // Any instances to close?
- if ((*globals)->delegateComponentInstance != 0L)
- {
- // Close the captured component instance
- result = CloseComponent ((*globals)->delegateComponentInstance);
- (*globals)->delegateComponentInstance = 0L;
-
- // Uncapture the captured component (make it visible again)
- result = UncaptureComponent ((*globals)->delegateComponent);
- (*globals)->delegateComponent = 0L;
- }
-
- // Dispose of globals
- DisposeHandle ((Handle) globals);
- }
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult NuMathCanDo (short selector)
- {
- switch (selector)
- {
- // Component Manager request codes
- case kComponentOpenSelect:
- case kComponentCloseSelect:
- case kComponentCanDoSelect:
- case kComponentVersionSelect:
- case kComponentRegisterSelect:
-
- // Math component request codes
- case kDoDivideSelect:
- case kDoMultiplySelect:
- {
- return (true);
- }
-
- // Unsupported request codes
- case kComponentTargetSelect:
- default:
- {
- return (false);
- }
- }
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult NuMathVersion (void)
- {
- // Return the version info
- return (nuMathInterfaceRevision);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult NuMathRegister (void)
- {
- // See if a Math component is registered - if not, don't
- // register this component, since it can't work without
- // the Math component. We return zero to register, one
- // to not register.
- ComponentDescription mathDesc;
-
- mathDesc.componentType = mathComponentType;
- mathDesc.componentSubType = 0L;
- mathDesc.componentManufacturer = 'appl';
- mathDesc.componentFlags = 0L;
- mathDesc.componentFlagsMask = 0L;
-
- return ((FindNextComponent (nil, &mathDesc) != 0L) ? 0L : 1L);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult NuMathDoDivide (short numerator,
- short denominator,
- short *quotient)
- {
- ComponentResult result = noErr;
-
- // Check for zero denominator
- if (denominator != 0)
- {
- *quotient = (short) Fix2Long
- (FixDiv (Long2Fix ((long) numerator), Long2Fix ((long) denominator)));
- }
- else // Divide by zero not allowed
- {
- *quotient = 0;
- result = kGenericError;
- }
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
- pascal ComponentResult NuMathDoMultiply (Handle storage,
- short firstNum,
- short secondNum,
- short *multiplicationResult)
- {
- PrivateGlobals** globals = (PrivateGlobals**) storage;
-
- // Note that we need access to the component globals because
- // we are delegating the multiply function to the captured
- // Math component, and the component instance of the captured
- // Math component is stored in the NuMath component globals. In
- // the MathDoMultiply function in the original Math component,
- // we didn't require access to the component globals, and the
- // interface for this function reflected this (there was no
- // storage parameter).
- return (DoMultiply ((*globals)->delegateComponentInstance, firstNum,
- secondNum, multiplicationResult));
- }
-
- //-----------------------------------------------------------------------
-
-
-